//
//  YuYueTableViewController.m
//  FD
//
//  Created by Yasin on 2018/10/13.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "YuYueListViewController.h"
//view
#import "YuYueCell.h"
#import "FDNowDataView.h" //无数据占位图

//contoller
#import "FDShopDetailController.h" //餐厅详情
//model
#import "YuYueModel.h"
@interface YuYueListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArr;
/**表头作为线*/
@property (nonatomic, strong) UIView *lightGrayLineHeaderView;
/**1-19无数据占位图默认隐藏*/
@property (nonatomic, strong) FDNowDataView *nodataView;

@end

@implementation YuYueListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self loadData];
}

- (void)setupUI {
    //导航条
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSLocalizedString(Home_Reservation, @"门店预约");
    
    self.tableView.tableHeaderView = self.lightGrayLineHeaderView;
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"YuYueCell" bundle:nil] forCellReuseIdentifier:@"YuYueCell"];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消分割线
    
    //添加无数据占位图
    [self.view addSubview:self.nodataView];
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.centerY.equalTo(self.view.mas_centerY);
        make.height.mas_equalTo(30);
    }];
    //默认隐藏
    self.nodataView.hidden = YES;
}


#pragma mark - getter
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (UIView *)lightGrayLineHeaderView {
    if (!_lightGrayLineHeaderView) {
        _lightGrayLineHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        _lightGrayLineHeaderView.backgroundColor = kMainLightGrayColor;
    }
    return _lightGrayLineHeaderView;
}

- (FDNowDataView *)nodataView {
    if (!_nodataView) {
        _nodataView = [[FDNowDataView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    }
    return _nodataView;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YuYueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YuYueCell"];
    YuYueModel *model = self.dataArr[indexPath.row];
    cell.yuyeuModel = model;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    FDShopDetailController *shopDetailVC = [[FDShopDetailController alloc] init];
    //取出店铺id
    YuYueModel *model = self.dataArr[indexPath.row];
    shopDetailVC.shopId = model.shopId;
    
    //拿到对一个的cell:点击预约按钮也可以跳转
    YuYueCell *cell = (YuYueCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.yuyueBtnBlock = ^{
        [self.navigationController pushViewController:shopDetailVC animated:YES];
    };
    
    [self.navigationController pushViewController:shopDetailVC animated:YES];
}

//!!!!:请求数据
- (void)loadData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [FDHttpAPi requestSYShopListSuccessBlock:^(id responseObjc) {
        [hud hideAnimated:YES];
        
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:@"SUCCESS"]) {
            
            NSArray *data = responseObjc[@"data"];
            if(data.count == 0) return;
            
            //字典转模型
            for (NSDictionary *dic in data) {
                YuYueModel *model = [YuYueModel mj_objectWithKeyValues:dic];
                //这个界面，需判断门店信息字段reservation是否=1，如果=1，则显示这个门店，=0则不显示
                if ([model.reservation isEqualToNumber:@1]) {
                    [self.dataArr addObject:model];
                }
            }
            
            //刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.dataArr.count) {
                    self.nodataView.hidden = YES;
                }else {
                    self.nodataView.hidden = NO;
                }
                [self.tableView reloadData];
            });
        }else {
            [JRToast showWithText:responseObjc[@"describe"]];
            self.nodataView.hidden = NO;
        }
    } failureBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        self.nodataView.hidden = NO;
        [hud hideAnimated:YES];
    }];
}

@end
